package com.ulook.lookplanet.ui.square.community;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.data.model.TCommunity;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.data.repository.CommunityRepository;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/20
 */
public class CommunityViewModel extends BaseViewModel2 {

    private CommunityRepository repository;
    private MutableLiveData<List<TCommunity>> communityList = new MutableLiveData<>();   // 小组列表
    private MutableLiveData<List<TTopic>> topicList = new MutableLiveData<>();           // 话题列表
    private TCommunity currCommunity;     // 当前选中的小组

    public CommunityViewModel(){
        repository = CommunityRepository.getInstance();
    }

    public LiveData<List<TCommunity>> getCommunityList(){
        return communityList;
    }
    public LiveData<List<TTopic>> getTopicList(){
        return topicList;
    }

    public TCommunity getCurrCommunity() {
        return currCommunity;
    }
    public void setCurrCommunity(TCommunity currCommunity) {
        this.currCommunity = currCommunity;
    }

    /**
     * 获取小组列表
     */
    public void getCommunityData(){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TCommunity> communities = JacksonUtil.json2list((String)res, TCommunity.class);
                    communityList.setValue(communities);
                } catch (Exception e) {
                    setToastText("获取小组解析数据失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("获取小组失败");
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        repository.getCommunityData(callback);
    }

    /**
     * 查询话题列表
     */
    public void getTopicsForComuId(String comuId){

        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TTopic> topics = JacksonUtil.json2list((String)res, TTopic.class);
                    topicList.setValue(topics);
                } catch (Exception e) {
                    setToastText("获取话题解析数据失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("获取话题失败");
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        repository.getTopicsForComuId(comuId, callback);
    }

}
